public class HashBucket2<k,v> {
    private static class Node<k,v>{
        k key;
        v value;
        Node next;

        public Node(k key, v value) {
            this.key = key;
            this.value = value;
        }
    }
    private  Node<k,v>[] arrary = (Node<k,v>[])new Node[10];
    private int usedSize;//当前数据存放个数
    private static final double LOAD_FACTOR = 0.75f;
    private static final int DEFAULT_SIZE = 8;//默认桶的大小

    public void put(k key,v val){
        int index = key.hashCode()%arrary.length;//找到该放入的下标
        //检查该下标的链表中是否有该key
        Node cur = arrary[index];
        while(cur!=null){
            if(cur.key.equals(key)){
                cur.value = val;
                return;
            }
            cur = cur.next;
        }
        //头插法插入该节点
        Node<k,v> node = new Node<k,v>(key,val);
        node.next = arrary[index];
        arrary[index] = node;
        usedSize++;
        //判断是否需要扩容
        if(doLoadFactor()>=LOAD_FACTOR){
            resize();
        }
    }
    private void resize(){
        Node<k,v>[] newArrary = (Node<k,v>[])new Node[2*arrary.length];
        //给每个节点重新分配位置
        for (int i = 0; i <arrary.length ; i++) {
            Node<k,v> cur = arrary[i];
            while(cur!=null){
                int index = cur.key.hashCode()% newArrary.length;
                Node<k,v> node = cur;
                node.next = newArrary[index];
                newArrary[index] = node;
                cur = cur.next;
            }
        }
        //更新新的数组
        arrary = newArrary;
    }
    private double doLoadFactor(){
        return usedSize*1.0/arrary.length;
    }
    //获取val的函数
    public v getVal(k key){
        int index = key.hashCode()%arrary.length;
        //遍历
        Node<k,v> cur = arrary[index];
        while(cur!=null){
            if(cur.key.equals(key)){
                return cur.value;
            }
            cur = cur.next;
        }
        return null;
    }


}
